<?
class UploadFile{

var $file_name;
var $file_type;
var $file_size;
var $file_source;
var $file_extension;
var $file_destiny;
var $error="";
var $estado_upload=true;

var $mensajes_error=array(
	'size'=>'Superado el limite de peso del archivo',
	'type'=>'El tipo de archivo no es el correcto',
	'ext'=>'La extencion del archivo no coincide con en tipo',
	'upload'=>'No es posible subir el archivo',
	'file'=>'Error al subir el Archivo',
	'dir'=>'La ruta del directorio no existe',
	'php'=>'Error al cargar archivo'
);

	/** @return void
	*  @param source $file -ejem; $_FILES[archivo]
	*/
	function UploadFile($file,$index=''){

		$this->error="";

		if(is_numeric($index)){
			$this->file_name=$file['name'][$index];
			$this->file_type=$file['type'][$index];
			$this->file_source=$file['tmp_name'][$index];
			$this->file_size=$file['size'][$index];
			$error=$this->file_error=$file['error'][$index];
		}
		else{
			$this->file_name=$file['name'];
			$this->file_type=$file['type'];
			$this->file_source=$file['tmp_name'];
			$this->file_size=$file['size'];
			$error=$this->file_error=$file['error'];
		}

		$error=$this->error_file_php($error);
		if($error){
			 $this->set_mensaje_error('php',$error);
			 $this->estado_upload=false;
		}
		$this->extension_archivo(); ///obtiene extension
	}


	/** @return boolean
	*  @param string $ruta, string $nombre_file
	*  @desc valida la ruta, y sis se puede subir el archivo, asigna un nombre si no se especifica alguno
	*/
	function upload_file($ruta,$nombre_file=''){

		if($this->estado_upload){
			///verificando ultimo slash('/')
			$ruta_aux=explode('/',$ruta);
			$ruta_aux_end=end($ruta_aux);

			if(empty($ruta_aux_end)){
				unset($ruta_aux[(count($ruta_aux)-1)]);
				$ruta=implode('/',$ruta_aux);
			}

			if(!$nombre_file){
				$nombre_file=str_replace(" ","",str_replace('.',"",microtime())).".";
			}

			$nombre_file.=$this->file_extension;

			if(!is_dir($ruta)){
				$this->estado_upload=false;
				$this->set_mensaje_error('dir');
			}
			elseif(!is_uploaded_file($this->file_source)){
				$this->estado_upload=false;
				$this->set_mensaje_error('upload');
			}
			elseif(!move_uploaded_file($this->file_source,$ruta.'/'.$nombre_file)){
				$this->estado_upload=false;
				$this->set_mensaje_error('file');
			}
			else{
				$this->estado_upload=true;
				$this->set_file_name($nombre_file);
			}
		}

		return $this->estado_upload;
	}

	/*
	 * @return bolean
	 * @param string $nombre, string $ruta
	 * @desc Crea una carpeta y le asigna los permisos de acceso completos (0777), en caso de existir habilita los permisos
	*/
	function crea_carpeta($p_nombre,$p_ruta)
	{
	   	$creo=true;

	   	if (!is_dir($p_ruta.$p_nombre))
	  	{
			$creo = mkdir($p_ruta.$p_nombre,0777);
		}
		else
		{
			$creo = chmod($p_ruta.$p_nombre,0777);
		}

		return $creo;
	}

	/** @return boolean
	*  @param string $size_lim
	*  @desc retorna error si se supero el maximo de tama�o permitido
	*/
	function valida_size($size_lim){

		if($file_size>$size_lim){
			$this->estado_upload=false;
			$this->set_mensaje_error('size');
		}
		else{
			$this->estado_upload=true;
		}

		return $this->estado_upload;
	}

	/** @return string
	*  @param array $extensiones_mime
	*  @desc valida si el MIME del archivo esta incluida en tre los permitidos
	*/
	function valida_type($extensiones_mime){

		$xts_mime=$this->extencion_mime($this->file_extension);

		if(!empty($xts_mime)&&$xts_mime!=$this->file_type){
			$this->estado_upload=false;
			$this->set_mensaje_error('ext',$txt);
		}
		elseif(!in_array($this->file_type,$extensiones_mime)){
			$this->estado_upload=false;
			$this->set_mensaje_error('type');
		}
		else{
			$this->estado_upload=true;
		}

		//print_r($extensiones_mime);
		echo $this->file_type;
		echo $this->file_extension;

		return $this->estado_upload;
	}

	function extension_archivo(){
		$punto = explode('.',$this->file_name);
		$this->file_extension=end($punto);
	}

	/** @return void
	*  @param string $fail
	*  @desc asigna valor del error a varible de clase
	*/
	function set_file_name($txt){
		$this->file_destiny=$txt;
	}

	/** @return string
	*  @desc retorna el error array de errores
	*/
	function get_file_name(){
		return $this->file_destiny;
	}

	/** @return string
	*  @param int $opc
	*  @desc retorna el valor de error impuesto por PHP al $_FILES[error]
	*/
	function error_file_php($opc){
		switch($opc){
			case 1: $error='UPLOAD_ERR_INI_SIZE - The uploaded file exceeds the
						upload_max_filesize directive in php.ini.';
					break;
			case 2; $error='UPLOAD_ERR_FORM_SIZE - The uploaded file exceeds the
						MAX_FILE_SIZE directive that was specified in the HTML form.';
					break;
			case 3: $error='UPLOAD_ERR_PARTIAL - The uploaded file was only
						partially uploaded.';
					break;
			case 4: $error='UPLOAD_ERR_NO_FILE - No file was uploaded.'; break;
			case 6: $error='UPLOAD_ERR_NO_TMP_DIR - Missing a temporary folder.';
					break;
			case 7: $error='UPLOAD_ERR_CANT_WRITE - Failed to write file to disk.';
					break;
			case 8: $error='UPLOAD_ERR_EXTENSION - File upload stopped by extension';
					 break;
		}
		return $error;
	}

	/** @return void
	*  @param string $fail
	*  @desc asigna valor del error a varible de clase
	*/
	function set_mensaje_error($fail,$txt=''){
		$this->error=$this->mensajes_error[$fail]." <br>".$txt;
	}

	/** @return string
	*  @desc retorna el error array de errores
	*/
	function get_mensaje_error(){
		return $this->error;
	}
	/** @return array
	*  @param array $extencion
	*  @desc Devuenve el tipo d MIME segun la extencion
	*/
	function extencion_mime($xts){

		$mime_types = array(
		  "323" => "text/h323",
		  "acx" => "application/internet-property-stream",
		  "ai" => "application/postscript",
		  "aif" => "audio/x-aiff",
		  "aifc" => "audio/x-aiff",
		  "aiff" => "audio/x-aiff",
		  "asf" => "video/x-ms-asf",
		  "asr" => "video/x-ms-asf",
		  "asx" => "video/x-ms-asf",
		  "au" => "audio/basic",
		  "avi" => "video/x-msvideo",
		  "axs" => "application/olescript",
		  "bas" => "text/plain",
		  "bcpio" => "application/x-bcpio",
		  "bin" => "application/octet-stream",
		  "bmp" => "image/bmp",
		  "c" => "text/plain",
		  "cat" => "application/vnd.ms-pkiseccat",
		  "cdf" => "application/x-cdf",
		  "cer" => "application/x-x509-ca-cert",
		  "class" => "application/octet-stream",
		  "clp" => "application/x-msclip",
		  "cmx" => "image/x-cmx",
		  "cod" => "image/cis-cod",
		  "cpio" => "application/x-cpio",
		  "crd" => "application/x-mscardfile",
		  "crl" => "application/pkix-crl",
		  "crt" => "application/x-x509-ca-cert",
		  "csh" => "application/x-csh",
		  "css" => "text/css",
		  "dcr" => "application/x-director",
		  "der" => "application/x-x509-ca-cert",
		  "dir" => "application/x-director",
		  "dll" => "application/x-msdownload",
		  "dms" => "application/octet-stream",
		  "doc" => "application/msword",
		  "dot" => "application/msword",
		  "dvi" => "application/x-dvi",
		  "dxr" => "application/x-director",
		  "eps" => "application/postscript",
		  "etx" => "text/x-setext",
		  "evy" => "application/envoy",
		  "exe" => "application/octet-stream",
		  "fif" => "application/fractals",
		  "flr" => "x-world/x-vrml",
		  "gif" => "image/gif",
		  "gtar" => "application/x-gtar",
		  "gz" => "application/x-gzip",
		  "h" => "text/plain",
		  "hdf" => "application/x-hdf",
		  "hlp" => "application/winhlp",
		  "hqx" => "application/mac-binhex40",
		  "hta" => "application/hta",
		  "htc" => "text/x-component",
		  "htm" => "text/html",
		  "html" => "text/html",
		  "htt" => "text/webviewhtml",
		  "ico" => "image/x-icon",
		  "ief" => "image/ief",
		  "iii" => "application/x-iphone",
		  "ins" => "application/x-internet-signup",
		  "isp" => "application/x-internet-signup",
		  "jfif" => "image/pipeg",
		  "jpe" => "image/jpeg",
		  "jpeg" => "image/jpeg",
		  "jpg" => "image/jpeg",
		  "js" => "application/x-javascript",
		  "latex" => "application/x-latex",
		  "lha" => "application/octet-stream",
		  "lsf" => "video/x-la-asf",
		  "lsx" => "video/x-la-asf",
		  "lzh" => "application/octet-stream",
		  "m13" => "application/x-msmediaview",
		  "m14" => "application/x-msmediaview",
		  "m3u" => "audio/x-mpegurl",
		  "man" => "application/x-troff-man",
		  "mdb" => "application/x-msaccess",
		  "me" => "application/x-troff-me",
		  "mht" => "message/rfc822",
		  "mhtml" => "message/rfc822",
		  "mid" => "audio/mid",
		  "mny" => "application/x-msmoney",
		  "mov" => "video/quicktime",
		  "movie" => "video/x-sgi-movie",
		  "mp2" => "video/mpeg",
		  "mp3" => "audio/mpeg",
		  "mpa" => "video/mpeg",
		  "mpe" => "video/mpeg",
		  "mpeg" => "video/mpeg",
		  "mpg" => "video/mpeg",
		  "mpp" => "application/vnd.ms-project",
		  "mpv2" => "video/mpeg",
		  "ms" => "application/x-troff-ms",
		  "mvb" => "application/x-msmediaview",
		  "nws" => "message/rfc822",
		  "oda" => "application/oda",
		  "p10" => "application/pkcs10",
		  "p12" => "application/x-pkcs12",
		  "p7b" => "application/x-pkcs7-certificates",
		  "p7c" => "application/x-pkcs7-mime",
		  "p7m" => "application/x-pkcs7-mime",
		  "p7r" => "application/x-pkcs7-certreqresp",
		  "p7s" => "application/x-pkcs7-signature",
		  "pbm" => "image/x-portable-bitmap",
		  "pdf" => "application/pdf",
		  "pfx" => "application/x-pkcs12",
		  "pgm" => "image/x-portable-graymap",
		  "pko" => "application/ynd.ms-pkipko",
		  "pma" => "application/x-perfmon",
		  "pmc" => "application/x-perfmon",
		  "pml" => "application/x-perfmon",
		  "pmr" => "application/x-perfmon",
		  "pmw" => "application/x-perfmon",
		  "pnm" => "image/x-portable-anymap",
		  "pot" => "application/vnd.ms-powerpoint",
		  "ppm" => "image/x-portable-pixmap",
		  "pps" => "application/vnd.ms-powerpoint",
		  "ppt" => "application/vnd.ms-powerpoint",
		  "prf" => "application/pics-rules",
		  "ps" => "application/postscript",
		  "pub" => "application/x-mspublisher",
		  "qt" => "video/quicktime",
		  "ra" => "audio/x-pn-realaudio",
		  "ram" => "audio/x-pn-realaudio",
		  "ras" => "image/x-cmu-raster",
		  "rgb" => "image/x-rgb",
		  "rmi" => "audio/mid",
		  "roff" => "application/x-troff",
		  "rtf" => "application/rtf",
		  "rtx" => "text/richtext",
		  "scd" => "application/x-msschedule",
		  "sct" => "text/scriptlet",
		  "setpay" => "application/set-payment-initiation",
		  "setreg" => "application/set-registration-initiation",
		  "sh" => "application/x-sh",
		  "shar" => "application/x-shar",
		  "sit" => "application/x-stuffit",
		  "snd" => "audio/basic",
		  "spc" => "application/x-pkcs7-certificates",
		  "spl" => "application/futuresplash",
		  "src" => "application/x-wais-source",
		  "sst" => "application/vnd.ms-pkicertstore",
		  "stl" => "application/vnd.ms-pkistl",
		  "stm" => "text/html",
		  "svg" => "image/svg+xml",
		  "sv4cpio" => "application/x-sv4cpio",
		  "sv4crc" => "application/x-sv4crc",
		  "t" => "application/x-troff",
		  "tar" => "application/x-tar",
		  "tcl" => "application/x-tcl",
		  "tex" => "application/x-tex",
		  "texi" => "application/x-texinfo",
		  "texinfo" => "application/x-texinfo",
		  "tgz" => "application/x-compressed",
		  "tif" => "image/tiff",
		  "tiff" => "image/tiff",
		  "tr" => "application/x-troff",
		  "trm" => "application/x-msterminal",
		  "tsv" => "text/tab-separated-values",
		  "txt" => "text/plain",
		  "uls" => "text/iuls",
		  "ustar" => "application/x-ustar",
		  "vcf" => "text/x-vcard",
		  "vrml" => "x-world/x-vrml",
		  "wav" => "audio/x-wav",
		  "wcm" => "application/vnd.ms-works",
		  "wdb" => "application/vnd.ms-works",
		  "wks" => "application/vnd.ms-works",
		  "wmf" => "application/x-msmetafile",
		  "wps" => "application/vnd.ms-works",
		  "wri" => "application/x-mswrite",
		  "wrl" => "x-world/x-vrml",
		  "wrz" => "x-world/x-vrml",
		  "xaf" => "x-world/x-vrml",
		  "xbm" => "image/x-xbitmap",
		  "xla" => "application/vnd.ms-excel",
		  "xlc" => "application/vnd.ms-excel",
		  "xlm" => "application/vnd.ms-excel",
		  "xls" => "application/vnd.ms-excel",
		  "xlt" => "application/vnd.ms-excel",
		  "xlw" => "application/vnd.ms-excel",
		  "xof" => "x-world/x-vrml",
		  "xpm" => "image/x-xpixmap",
		  "xwd" => "image/x-xwindowdump",
		  "z" => "application/x-compress",
		  "zip" => "application/zip"
		);

		return $array_mime[$xts];
	}

}
?>
